package com.eascs.ticket.configuration.controller;

import javax.inject.Inject;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.common.util.ObjectUtil;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.ticket.common.controller.BaseActionRestController;
import com.eascs.ticket.common.entity.EntityUtil;
import com.eascs.ticket.common.form.IdForm;
import com.eascs.ticket.configuration.entity.Workday;
import com.eascs.ticket.configuration.entity.WorkdayUpdate;
import com.eascs.ticket.configuration.form.BuildDayForm;
import com.eascs.ticket.configuration.form.WorkdayUpdateForm;
import com.eascs.ticket.configuration.service.WorkdayService;

@RestController
public class WorkdayRestController extends BaseActionRestController {
	@Inject
	private WorkdayService workdayService;

	@RequestMapping(value = "/workday/buildDay")
	public BaseRestResult<Integer> buildDay(BuildDayForm buildDayForm) {
		return BaseRestResult.buildRestResult(workdayService.buildDay(buildDayForm.getFrom(), buildDayForm.getTo()));
	}

	@RequestMapping(value = "/workday/updateWorkday")
	public BaseRestResult<String> updateWorkday(WorkdayUpdateForm workdayUpdateForm) {
		WorkdayUpdate workdayUpdate = new WorkdayUpdate();
		ObjectUtil.copySimpleProperties(workdayUpdateForm, workdayUpdate);
		EntityUtil.initUpdateEntity(workdayUpdate);
		workdayService.updateWork(workdayUpdate);
		return BaseRestResult.refresh();
	}

	@RequestMapping(value = "/workday/getUpdateWorkdayView")
	public BaseRestResult<String> getUpdateWorkdayView(IdForm idForm) {
		Workday workday = this.workdayService.getWorkday(idForm.getId());
		ViewContext.put("workday", workday);
		return HtmlRestResult.popHtmlFragment("configuration/workday/getUpdateWorkdayView");
	}

}
